/*This is about the parser of the XML result of Yahoo Boss API.
 * After sending doGet request to Yahoo API, the user would receive
 * the response from Yahoo. We need to parser the return result.
 * Show the result to user, and the user would based on the title, summary
 * to decide whether the corresponding result is relevant to the target.
 * After the user have give his/her feedback,we would keep all the relevant
 * return result's index. And also, we would keep all the return result in input/
 * directory. And then the IfIdf would based on that corpus to build tf_idf index.
 * 
 * */

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import javax.xml.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.*;
import org.xml.sax.SAXException;
public class XML {	
	
	public static ReqResult reqResp(String query, String apiT, String site) throws IOException, SAXException, ParserConfigurationException{
		
		String api="l1KSSDnV34Gujbi63Gu5WX52uIHc4EyC1JFL.A4PrreoDGIKAuA7qGMa4YPeWPpoxyI";
		String request="http://boss.yahooapis.com/ysearch/web/v1/"+query+"?appid="+api+"&count=4&format=xml&sites="+site;
       
		ReqResult result=new ReqResult();
        
		URL url=null;
		try {
			url = new URL(request);
			
			
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			System.out.println(query);
		}
		
        
		InputStream rstream= url.openStream();
		Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(rstream);  
        NodeList nodeList = doc.getElementsByTagName("resultset_web");
        Node node=nodeList.item(0);
        Element element = (Element) node;
        String my= element.getAttribute("totalhits");
        int count=Integer.parseInt(my); 
        result.addCount(count);
        
        NodeList nodeList2=doc.getElementsByTagName("result");
        
        int length= nodeList2.getLength()==4? 4 :nodeList2.getLength();
        for(int index=0;index<length;index++){
        	String URL=null;
        	
        	NodeList nameNode = element.getElementsByTagName("url");

			if (nameNode.item(0).getNodeType() == Node.ELEMENT_NODE) {
				Element nameElement = (Element) nameNode.item(0);
				URL = nameElement.getFirstChild().getNodeValue().trim();
		
			}
			
            result.addURL(URL);
        	result.setQuery(query);
        }
        return result;
        
	}
	
	public static void main(String args []) throws IOException, SAXException, ParserConfigurationException{
		
		String query="japan";
		String api="l1KSSDnV34Gujbi63Gu5WX52uIHc4EyC1JFL.A4PrreoDGIKAuA7qGMa4YPeWPpoxyI";
		String site="cnn.com";
		
		
		reqResp(query,api, site);
		
	}
	
}
